package com.bykj.property.api.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bykj.property.api.service.WxAppRepairService;
import com.bykj.property.api.vo.PmProprietorVo;
import com.bykj.property.controller.vo.view.RepairOrderVo;
import com.bykj.property.model.AppPropOpen;
import com.bykj.property.model.PmProprietor;
import com.bykj.property.model.RepairOrder;
import com.bykj.property.model.RepairOrderImage;
import com.bykj.property.service.AppPropOpenService;
import com.bykj.property.service.PmProprietorService;
import com.bykj.property.service.RepairOrderImageService;
import com.bykj.property.service.RepairOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@Service
@Slf4j
public class WxAppRepairServiceImpl implements WxAppRepairService {

    @Autowired
    private AppPropOpenService appPropOpenService;
    @Autowired
    private PmProprietorService pmProprietorService;
    @Autowired
    private RepairOrderService repairOrderService;
    @Autowired
    private RepairOrderImageService repairOrderImageService;
    @Override
    public int submitRepairOrder(@NotEmpty String openid,
                                 @NotEmpty String repair_content,
                                 String repair_name,
                                 String phone,
                                 String img) {
        AppPropOpen appPropOpen = appPropOpenService.getById(openid);
        if (appPropOpen!=null&&appPropOpen.getPmProprietorId()!=null){
            RepairOrder repairOrder = new RepairOrder();
            PmProprietor pmProprietor = pmProprietorService.getById(appPropOpen.getPmProprietorId());
            repairOrder.setPhone(phone);
            repairOrder.setPmProprietorId(pmProprietor.getPmProprietorId());
            repairOrder.setRepairContent(repair_content);
             int saveRepair = repairOrderService.saveTokey(repairOrder);
             log.info("img==="+img);
             int saveImg = 1;
             if (StringUtils.isNotEmpty(img)) {
                 RepairOrderImage image = new RepairOrderImage();
                 image.setImageUrl(img);
                 image.setRepairOrder(repairOrder.getRepairOrderId());
                 saveImg = repairOrderImageService.save(image) == true ? 1 : 0;
             }
             return saveRepair == 1 && saveImg == 1 ? 1 : 0;
        }else {
            return 0;
        }
    }

    @Override
    public List<RepairOrder> repairGetOrders(@NotEmpty String openid) {
        AppPropOpen appPropOpen = appPropOpenService.getById(openid);
        RepairOrder order = new RepairOrder();
        if(appPropOpen.getPmProprietorId()!=null){
            order.setPmProprietorId(appPropOpen.getPmProprietorId());
            QueryWrapper<RepairOrder> orderQueryWrapper = new QueryWrapper<>();
            orderQueryWrapper.setEntity(order);
            List<RepairOrder> repairOrders = repairOrderService.list(orderQueryWrapper);
            return repairOrders;

        }else{
            return null;
        }
    }


    @Override
    public RepairOrderVo getRepairOrderDetail(@NotEmpty String repairOrderId) {
        RepairOrder repairOrder = repairOrderService.getById(repairOrderId);
        RepairOrderVo repairOrderVo = JSON.parseObject(JSON.toJSONString(repairOrder),RepairOrderVo.class);

        RepairOrderImage repairOrderImage = new RepairOrderImage();
        repairOrderImage.setRepairOrder(Long.valueOf(repairOrderId));
        QueryWrapper<RepairOrderImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(repairOrderImage);
        RepairOrderImage repairOrderImageDB = repairOrderImageService.getOne(queryWrapper);
        if (repairOrderImageDB!=null)
            repairOrderVo.setImg(repairOrderImageDB.getImageUrl());
        return repairOrderVo;
    }
}
